/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ar.com.remiseria.bo;

import ar.com.remiseria.dao.AutoDAO;
import ar.com.remiseria.entities.Auto;
import ar.com.remiseria.entities.Chofer;
import ar.com.remiseria.utils.Constantes;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;

/**
 *
 * @author mcvalls
 * 
 * Aqui va toda la logica de negocios correspondiente a los automoviles
 */
public class AutoBO {
    
    public List<Auto> getAllAutos() throws Exception{
        List<Auto> losAutos = new ArrayList<Auto>();
        
        try{
            AutoDAO autoDAO = new AutoDAO();
            losAutos = autoDAO.getAll(Auto.class);
        }catch(HibernateException hex){
            throw new Exception(hex);
        }
        
        return losAutos;
    }

    public List<Auto> getAutosByChofer(Chofer chofer) throws Exception{
        
        List<Auto> losAutos;
        AutoDAO autoDAO = new AutoDAO();
        
        try{
            losAutos = autoDAO.getAutosByChofer(chofer);
        }catch(HibernateException hex){
            throw new Exception(hex);
        }
        
        return losAutos;
        
    }

    public void delete(Auto auto) throws Exception {
        AutoDAO autoDAO = new AutoDAO();
        try{
            autoDAO.delete(auto);
        }catch(HibernateException ex){
            throw new Exception(ex);
        }
    }

    public Auto saveNewAutomovil(Auto auto) throws Exception {
        AutoDAO autoDAO = new AutoDAO();
        try{
            auto = autoDAO.saveNew(auto);
        }catch(HibernateException hex){
            throw new Exception(hex);
        }
        
        return auto;
    }

    public Auto update(Auto auto) throws Exception{
        AutoDAO autoDAO = new AutoDAO();
        try{
            auto = autoDAO.update(auto);
        }catch(HibernateException ex){
            throw new Exception(ex);
        }
        
        return auto;
    }

    public List<Auto> getAllAutos(int paginaActual) throws Exception {
        List<Auto> losAutos = new ArrayList<Auto>();
        
        int start = 0;
        
        if(paginaActual > 1){
            
            start = ((paginaActual - 1) * Constantes.PAG_MAX_RESULTS) + 1;
            
        }
        
        try{
            AutoDAO autoDAO = new AutoDAO();
            losAutos = autoDAO.getAll(Auto.class, start);
        }catch(HibernateException hex){
            throw new Exception(hex);
        }
        
        return losAutos;
    }

    public int getAutosCount() throws Exception {
        int autosTotales = 0;
        AutoDAO autoDAO = new AutoDAO();
        
        try{
            autosTotales = autoDAO.getCount(Auto.class);
        }catch(HibernateException ex){
            throw new Exception(ex);
        }
        
        return autosTotales;
    }
    
}
